﻿/*
 * Author: .net follower (http://dotnetfollower.com)
 * Copyright: � 2012 .net follower 
 * Usage: software is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
 */

using System;
using System.Net;
using System.IO;

namespace WebLoading
{
    /// <summary>    
    /// Abstracts the data loading from a specified source using Http. Provides such basic steps as data loading, verification and interpretation.
    /// </summary>
    /// <typeparam name="TRawData">A type of data to be loaded</typeparam>
    /// <typeparam name="TParsedData">A type of the analyzed data to be returned</typeparam>
    public abstract class HttpLoader<TRawData, TParsedData> : BaseLoader<TRawData, TParsedData>
        where TRawData    : class
        where TParsedData : class
    {
        /// <summary>
        /// An initial url to load data from
        /// </summary>
        protected readonly string _requestUrl;

        /// <summary>
        /// Initializes a new instance of the HttpLoader class
        /// </summary>
        /// <param name="requestUrl">An initial url to load data from</param>
        public HttpLoader(string requestUrl)
        {
            _requestUrl = requestUrl;
        }

        /// <summary>
        /// Initializes a new instance of the HttpLoader class
        /// </summary>
        public HttpLoader()
        {            
        }

        /// <summary>       
        /// Loads data from a specified source using Http, returns a ready-to-analyze data view
        /// </summary>
        /// <returns>A ready-to-analyze view of the data</returns>
        protected override TRawData LoadRawData()
        {
            HttpWebRequest httpWebRequest = PrepareRequest();

            TRawData rawData;
            using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                using (Stream responseStream = httpWebResponse.GetResponseStream())
                    rawData = ReadResponseStream(responseStream);

            return rawData;
        }

        /// <summary>
        /// Creates and initializes a Http request object
        /// </summary>
        /// <returns>A Http request object</returns>
        protected virtual HttpWebRequest PrepareRequest()
        {
            string url = PrepareUrl();

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.UserAgent      = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
            return httpWebRequest;
        }

        /// <summary>
        /// Prepares and returns an url the data should be loaded from. If the parameterless constructor is used, the method has to be overridden in a derived class
        /// </summary>
        /// <returns>An url to load the data from</returns>
        protected virtual string PrepareUrl()
        {
            return _requestUrl;
        }

        /// <summary>
        /// Reads from a stream and returns the received data in a ready-to-analyze view. Has to be overridden in a derived class
        /// </summary>
        /// <param name="stream">A data stream</param>
        /// <returns>The ready-to-analyze view of the data</returns>
        protected abstract TRawData ReadResponseStream(Stream stream);        
    }
}
